package com.qifan.alb2b.action.back;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qifan.alb2b.action.back.param.HotelQueryParam;
import com.qifan.alb2b.action.back.param.RoomQueryParam;
import com.qifan.alb2b.domain.HotelInfoDomain;
import com.qifan.alb2b.domain.RoomInfoDomain;
import com.qifan.alb2b.service.RoomInfoService;
import com.qifan.alb2b.util.DataGridModel;
import com.qifan.alb2b.util.ID;
import com.qifan.alb2b.util.Page;

@Controller
@RequestMapping("/admin/room")
public class RoomManageController{
	private RoomInfoService roomInfoService;

	public void setroomInfoService(RoomInfoService roomInfoService) {
		this.roomInfoService = roomInfoService;
	}
	
	@RequestMapping("/query.adm")
	@ResponseBody
	public Page query(DataGridModel dgm,RoomQueryParam uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		return roomInfoService.query(dgm,uqp);
	}
	
	@RequestMapping("/queryAllHotel.adm")
	@ResponseBody
	public List<HotelInfoDomain> queryAllHotel(DataGridModel dgm,HotelQueryParam uqp) {
		//spring太给力了，可以自动装配两个对象  会自动的装返回的Map转换成JSON对象
		dgm.setRows(Integer.MAX_VALUE);
		dgm.setPage(1);
		Page page= roomInfoService.queryAllHotel(dgm, uqp);
		return page.getRows();
	}
	
	@RequestMapping("/add.adm")
	@ResponseBody
	public Map<String, Object> add(RoomInfoDomain roomInfoDomain){
		Map<String, Object> map=new HashMap<String, Object>();
		try{
			roomInfoDomain.setRoomId(ID.next()+"");//设置唯一ID
			
			roomInfoDomain.setUnit(roomInfoDomain.getUnit());
			roomInfoDomain.setProfit(roomInfoDomain.getSellPrice()-roomInfoDomain.getCostPrice());
			roomInfoDomain.setPercent(Math.round((roomInfoDomain.getSellPrice()-roomInfoDomain.getCostPrice())/roomInfoDomain.getSellPrice()*100));
			roomInfoService.add(roomInfoDomain);
			map.put("success", true);
		}catch(RuntimeException e){
			map.put("success", false);
			map.put("msg", "操作失败,"+e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/update.adm")
	@ResponseBody
	public Map<String, Object> update(RoomInfoDomain roomInfoDomain){
		Map<String, Object> map=new HashMap<String, Object>();
		try{
			roomInfoDomain.setProfit(roomInfoDomain.getSellPrice()-roomInfoDomain.getCostPrice());
			roomInfoDomain.setPercent(Math.round((roomInfoDomain.getSellPrice()-roomInfoDomain.getCostPrice())/roomInfoDomain.getSellPrice()*100));
			roomInfoService.update(roomInfoDomain);
			map.put("success", true);
		}catch(RuntimeException e){
			map.put("success", false);
			map.put("msg", "操作失败,"+e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/del.adm")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("uid") String[] ids){
		Map<String, Object> map=new HashMap<String, Object>();
		try{
			roomInfoService.delete(ids);
			map.put("success", true);
		}catch(RuntimeException e){
			map.put("success", false);
			map.put("msg", "操作失败,"+e.getMessage());
		}
		return map;
	}
	
}
